package com.javalearn.gulimail.order.web;

import com.javalearn.common.exception.NoStockException;
import com.javalearn.gulimail.order.service.OrderService;
import com.javalearn.gulimail.order.vo.OrderConfirmVo;
import com.javalearn.gulimail.order.vo.OrderSubmitVo;
import com.javalearn.gulimail.order.vo.SubmitOrderResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.descriptor.web.WebXml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.concurrent.ExecutionException;

@Controller
@Slf4j
public class OrderWebController {
    @Autowired
    OrderService orderService;

    @GetMapping("/toTrade")
    public String toTrade(Model model, HttpSession session) throws ExecutionException, InterruptedException {
        OrderConfirmVo confirmVo = orderService.confirmOrder();
        model.addAttribute("orderConfirmData",confirmVo);
        return "confirm";
    }

    /**
     * 下单功能
     * @param
     * @return
     */
    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo vo, Model model, RedirectAttributes redirectAttributes){
//        SubmitOrderResponseVo responseVo = orderService.submitOrder(vo);
//
//        //下单成功，来到支付选择页
//
//        //下单失败回到订单确认页重新确认订单信息
//        System.out.println(vo);
//        if (responseVo.getCode()==0){
//            //下单成功来带支付选择页
//            model.addAttribute("submitOrderResp",responseVo);
//            return "pay";
//        }else {
//            String msg = "下单失败";
//            switch (responseVo.getCode()){
//                case 1: msg+="token failed";break;
//                case 2: msg+="price failed";break;
//                case 3: msg+="ware failed";break;
//            }
//            redirectAttributes.addFlashAttribute("msg",msg);
//            log.info("错误原因{}",msg);
//            return "redirect:http://order.gulimail.com/toTrade";
//        }

        try {
            SubmitOrderResponseVo responseVo = orderService.submitOrder(vo);
            if (responseVo.getCode()==0){
                //下单成功来带支付选择页
                model.addAttribute("submitOrderResp",responseVo);
                return "pay";
            }else {
                String msg = "下单失败";
                switch (responseVo.getCode()){
                    case 1: msg+="token failed";break;
                    case 2: msg+="price failed";break;
                    case 3: msg+="ware failed";break;
                }
                redirectAttributes.addFlashAttribute("msg",msg);
                log.info("错误原因{}",msg);
                return "redirect:http://order.gulimail.com/toTrade";
            }
        }catch (Exception e){
            if (e instanceof NoStockException){
                String message = ((NoStockException) e).getMessage();
                redirectAttributes.addFlashAttribute("mag",message);
            }
            return "redirect:http://order.gulimail.com/toTrade";
        }
    }
}
